<?php
namespace Ekadmin\Controller;

use \Think\Controller;
use Think\Page;

class OrderController extends CommonController
{

    public function index()
    {
        $param = I('get.');
        
        $where = " uniacid=" . $this->uniacid;

        
        if ($param['is_pay']) {

            $where .= " and is_pay=" . $param['is_pay'];
        }
        if ($param['is_send']) {

            $where .= " and is_send=" . $param['is_send'];
        } 
        if ($param['is_complete']) { 
            $where .= " and is_complete=" . $param['is_complete'];
        }
        if ($param['is_del']) { 
            $where .= " and is_del=" . $param['is_del'];
        }
        if ($param['is_comment']) { 
            $where .= " and is_comment=" . $param['is_comment'];
        }
        if ($param['tuan_uid']) {
            
            
            $where .= " and tuan_uid=" . $param['tuan_uid'];
        }
        if ($param['quyu_id']) {
            
            $where .= " and quyu_id=" . $param['quyu_id'];
        }
        if ($param['xiaoqu_id']) { 
            $where .= " and xiaoqu_id=" . $param['xiaoqu_id'];
            $this->xiaoqu = M("xiaoqu")->field("id,name")
                ->order("id desc")
                ->where("quyu_id=" . $param['quyu_id'])
                ->select();
        }
        if ($param['activity_id']) { 
            $where .= " and activity_id=" . $param['activity_id'];
        }
        $this->activity = M("activity")->field("id,name")
            ->order("id desc")
            ->select();
        if ($param['out_trade_no']) {
            
            $where .= " and out_trade_no like '%" . $param['out_trade_no'] . "%'";
        }
        if ($param['mobile']) {
            
            $where .= " and mobile='" . $param['mobile'] . "'";
        }
        
        if ($param['name']) {
            $where .= " and name='" . $param['name'] . "'";
        }
        if ($param['start'] != "") {
            $where .= " and addtime>=" . strtotime(date("Y-m-d 00:00:00", strtotime($param['start'])));
        }
        if ($param['end'] != "") {
            
            $where .= " and addtime<=" . strtotime(date("Y-m-d 23:59:59", strtotime($param['end'])));
        }

        $obj = M('order');
        $count = $obj->where($where)->count();
        $this->assign('count', $count);
        $page = new Page($count);
        $data = $obj->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order('id desc')
            ->select(); 
        foreach ($data as $k => $v) {
            $xiaoqu = M("xiaoqu")->where("uniacid=".$this->uniacid." and id=".$v["xiaoqu_id"])->find();
            
            $data[$k]["tuan_name"] = $xiaoqu["tuan_name"] . "<br>" . $xiaoqu["mobile"];
            
            $user = M('user')->where([
                'id' => $v['uid']
            ])
                ->field('nickname,head,mobile')
                ->find(); 
            $data[$k]['goodsinfo'] = json_decode($v['goodsinfo'], true);
            $data[$k]['user'] = $user;

            if ($data[$k]["is_tui"] == 1) {
                $data[$k]["is_tuikan"] = "没有退款";
                
                // $rcount=M("refund")->where("out_trade_no='".$data[$k]["out_trade_no"]."' and status<>3")->count();
                // if(!$rcount){
                
                // $data[$k]["is_tuikan"]="未退款";
                
                // }else{
                // if($data[$k]["pay_jiner"]>$data[$k]["refund_jiner"]){
                // if($data[$k]["refund_jiner"]>0){
                // $data[$k]["is_tuikan"]="部分退款";
                // }else{
                // $data[$k]["is_tuikan"]="申请退款中";
                // }
                // }else{
                // $data[$k]["is_tuikan"]="已退款";
                // }
                
                // }
            } else {
                
                if ($data[$k]["tui_status"] == 2) {
                    
                    $data[$k]["is_tuikan"] = "已退款";
                }else{
                    $data[$k]["is_tuikan"] = "不同意退款";
                }
                // else{
                // $rcount=M("refund")->where("out_trade_no='".$data[$k]["out_trade_no"]."' and status<>3")->count();
                // if($rcount){
                // $data[$k]["is_tuikan"]="已处理[<font color='red'>部分退款</font>]";
                // }else{
                // $data[$k]["is_tuikan"]="已处理";
                // }
                // }
            }
        }
        $this->quyu = M("quyu")->field('id,name')
            ->order('id desc')->where("uniacid=".$this->uniacid)
            ->select();
        // dump($data);die;
        $this->assign('data', $data);
        $this->assign('page', $page->show());
        $this->display();
    }



    /**
     * *@订单批量删除操作
     */
    public function del()
    {
        if (IS_POST) {
            $param = I('id');
            $map=array();
            if (is_array($param)) {
                $map['id'] = array('in',$param);
            } else {
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            M('order')->where($map)->delete();
            ajax_return(0, '删除成功');
        }
    }

    /**
     * @订单放入回收站
     */
    public function del2()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array(
                    'in',
                    $param
                );
            } else {
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            M('order')->where($map)->save([
                'is_del' => 1
            ]);
            
            ajax_return(0, '删除成功');
        }
    }

    /*
     * @订单恢复成功
     */
    public function del3()
    {
        if (IS_POST) {
            $param = I('id');
            
            if (is_array($param)) {
                $map['id'] = array(
                    'in',
                    $param
                );
            } else {
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            M('order')->where($map)->save([
                'is_del' => 0
            ]);
            
            ajax_return(0, '恢复成功');
        }
    }
    
    // 导出当前条件的所有订单
    public function exportOrder()
    {
        $param = I('get.');
        
        $where ="uniacid =". $this->uniacid;

        if($param['quyu_id']){

            $where.=" and quyu_id='".$param['quyu_id']."'";

        }
        
        if($param['xiaoqu_id']){
        
            $where.=" and xiaoqu_id='".$param['xiaoqu_id']."'";
        
        }
        
        if($param['activity_id']){
        
            $where.=" and activity_id='".$param['activity_id']."'";
        
        } 
        
        if($param['is_pay']){
        
            $where.=" and is_pay='".$param['is_pay']."'";
        
        }       
        
        
        if($param['is_send']){

            $where.=" and is_send='".$param['is_send']."'";

        }

        
        if($param['is_tui']){
        
            $where.=" and is_tui='".$param['is_tui']."'";
        
        }
        

        if($param['is_complete']){
        
            $where.=" and is_complete='".$param['is_complete']."'";
        
        }
        
        if($param['is_del']){
        
            $where.=" and is_del='".$param['is_del']."'";
        
        }
        
        if($param['is_comment']){
        
            $where.=" and is_comment='".$param['is_comment']."'";
        
        }


        if($param['out_trade_no']){

            $where.=" and out_trade_no='".$param['out_trade_no']."'";

        }

        if($param['mobile']){
        
            $where.=" and mobile='".$param['mobile']."'";
        
        }

        if($param['name']){

            $where.=" and name='".$param['name']."'";//收件人

        }
        
        if($param['start']!=""){
            $where.=" and addtime>=".strtotime(date("Y-m-d 00:00:00",strtotime($param['start'])));
        }
        
        if($param['end']!=""){
        
            //$where['addtime'] = array('lt',strtotime($param['end']));
            $where.=" and addtime<=".strtotime(date("Y-m-d 23:59:59",strtotime($param['end'])));
        }        
        
        
        $xlsCell = array(
            [
                'quyu',
                '线路'
            ],
            
            [
                'xiaoqu',
                '小区名称'
            ],
            
            [
                'active',
                '活动名称'
            ],
            
            [
                'tuan_name',
                '团长姓名'
            ],
            [
                'out_trade_no',
                '订单编号'
            ],
            [
                'uid',
                '会员信息'
            ],
            
            [
                'total_jiner',
                '订单总金额'
            ],
            [
                'goods',
                '订单商品'
            ],
            [
                'addtime',
                '下单时间'
            ],
            [
                'name',
                '收货人'
            ],
            [
                'mobile',
                '联系方式'
            ],
            [
                'address',
                '地址'
            ],
            [
                'is_pay',
                '支付状态'
            ],
            [
                'is_send',
                '发货状态'
            ],
            [
                'peisong_type',
                '配送类型'
            ]
        ); // ->field('out_trade_no,uid,total_jiner,addtime,name,mobile,address,goods_id,is_pay,is_send,goodsinfo')
        $data = M('order')->where($where)
            ->order("id desc")
            ->select();
        
        // echo "<pre>";print_r($data);echo "</pre>";exit;
        
        $goodsobj = M('goods');
        foreach ($data as $k => $v) {
            $rs = M("quyu")->where("id=".$v["quyu_id"]." and uniacid=".$this->uniacid)->find();
            
            $data[$k]['quyu'] = $rs["name"];
            
            $rs = M("xiaoqu")->where(" id=".$v["xiaoqu_id"]." and uniacid=".$this->uniacid)->find();
            
            $data[$k]["xiaoqu"] = $rs["name"];
            
            $data[$k]["tuan_name"] = $rs["tuan_name"];
            
            $rs = M("activity")->where("id=".$v["activity_id"]." and uniacid=".$this->uniacid)->find();
            
            $data[$k]["active"] = $rs["name"];
            
            $data[$k]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            $data[$k]['out_trade_no'] = "\t" . $v['out_trade_no'] . "\t";
            $data[$k]['address'] = $v['address'];
            $data[$k]['uid'] = $v['name']; // .' | 会员ID:'.$v['uid']
            $data[$k]['is_pay'] = $v['is_pay'] == 1 ? '未支付' : '已支付';
            $data[$k]['is_send'] = $v['is_send'] == 1 ? '未发货' : '已发货';
            switch($data[$k]['peisong_type']){
                case 1:
                    $data[$k]['peisong_type']="自提";
                    break;
                case 2:
                    $data[$k]['peisong_type']="送货上门";
                    break;
                default:
                    $data[$k]['peisong_type']="自定义";
                    break;
            }
            
            $goodsinfo = json_decode($v['goodsinfo'], true);
            
            $str = "";
            foreach ($goodsinfo as $kk => $vs) {
                
                $rets = $goodsobj->where("id=".$vs["id"]." and uniacid=".$this->uniacid)->find();
                if ($str == "") {
                    $str .= $rets["name"] . "*" . $vs["num"];
                } else {
                    $str .= ',' . $rets["name"] . "*" . $vs["num"];
                    // $str.=',商品Id：'.$vs["id"].' 商品名称：'.$rets["name"].","."团购价格".$vs["tuan_price"]." 购买数量".$vs["num"];
                }
            }
            $data[$k]['goods'] = $str;
        }
        
        if ($data) {
            if ($param['start'] && $param['end']) {
                $title = $param['start'] . '--' . $param['end'] . '期间订单';
            } else {
                $title = date("YmdHis") . "期间订单";
            }
            
            exportExcel($title, $xlsCell, $data);
        } else {
            
            echo "<script>alert('没有记录');</script>";
            exit();
        }
    }

    /**
     * *@通过线路改变获取小区信息
     */
    public function getXiaoqu()
    {
        if (! isset($_POST["quyu_id"]) || intval($_POST["quyu_id"]) <= 0) {
            $data = array(
                "retCode" => '0001',
                "retDesc" => "非法操作"
            );
            exit(json_encode($data));
        }
        $quyu_id = intval($_POST["quyu_id"]);
        $list = M("xiaoqu")->field('id,name')
            ->where('quyu_id=' . $quyu_id." and uniacid=".$this->uniacid)
            ->select();
        $str = "";
        if ($list) {
            for ($i = 0; $i < count($list); $i ++) {
                $str .= "<option value='" . $list[$i]["id"] . "'>" . $list[$i]["name"] . "</option>";
            }
            $data = array(
                "retCode" => '0000',
                "retDesc" => "获取成功",
                "data" => $str
            );
        } else {
            $data = array(
                "retCode" => '0001',
                "retDesc" => "没有数据"
            );
        }
        exit(json_encode($data));
    }

    /**
     * *@发货操作
     */
    public function isSend()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array('in',$param);
            } else {
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            $map["is_del"]=1;
            
            $map["is_pay"]=2;
            
            $arr = array(
                "is_send" => 2,
                "send_time" => time()
            );
            M('order')->where($map)->setField($arr);
            ajax_return(0, '批量发货操作成功');
        }
    }

    /**
     * *查看订单详情操作
     */
    public function view()//做到这里了
    {
        $data = M('order')->where("id=".I("id")." and uniacid=".$this->uniacid)->find();
        if ($data) {
            $rs = M("user")->where("id=".$data["uid"]." and uniacid=".$this->uniacid)->find();
            $data["acv"] = $rs["head"];
            $data["nickname"] = $rs["nickname"];
            if ($data["is_use_quan"] == 2) {
                $data["is_use_quan1"] = "使用优惠券";
                $rs = M("member_quan")->find($data["quan_id"]);
                switch ($rs["quan_type"]) {
                    case 1:
                        $rs["quan_type"] = "通用券";
                        break;
                    case 2:
                        $rs["quan_type"] = "满减券";
                        break;
                    case 3:
                        $rs["quan_type"] = "指定商品";
                }
                $data["quan_id"] = $rs;
            } else {
                $data["is_use_quan1"] = "未使用优惠券";
                $data["quan_id"] = array();
            }
            $rs = M("xiaoqu")->find($data["xiaoqu_id"]);
            if ($rs) {
                $data["xiaoqu_id"] = $rs["name"];
            } else {
                $data["xiaoqu_id"] = "无";
            }
            $rs = M("activity")->find($data["activity_id"]);
            if ($rs) {
                $data["activity_id"] = $rs["name"];
            } else {
                $data["activity_id"] = "无";
            }
            $rs = M("quyu")->find($data["quyu_id"]);
            if ($rs) {
                $data["quyu_id"] = $rs["name"];
            } else {
                $data["quyu_id"] = "无";
            }
            $data["goodsinfo"] = json_decode($data["goodsinfo"], true);
        } else {
            ajax_return(1, '没有数据');
        }
        $this->assign('data', $data);
        $this->display('view');
    }

    /**
     * @会员申请退款列表
     */
    public function refundindex()
    {
        $param = I('get.');
        
        $where["uniacid"]=$this->uniacid;
        
        if ($param['status']) {
            $where['status'] = $param['status'];
        }
        
        if ($param['uid']) {
            $where['uid'] = $param['uid'];
        }
        
        if ($param['out_trade_no']) {
            $where['out_trade_no'] = array(
                "like",
                "%" . $param['out_trade_no'] . "%"
            );
        }
        
        $obj = M('refund');
        
        $count = $obj->where($where)->count();
        
        $this->assign('count', $count);
        
        $page = new Page($count);
        
        $data = $obj->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order('id desc')
            ->select();
        
        if ($data) {
            for ($i = 0; $i < count($data); $i ++) {
                $rs = M("order")->field("id,pay_jiner,mobile,goodsinfo")
                    ->where("out_trade_no='" . $data[$i]["out_trade_no"] . "'")
                    ->find();
                $data[$i]["pay_jiner"] = $rs["pay_jiner"];
                $data[$i]["mobile"] = $rs["mobile"];
                $data[$i]['goodsinfo'] = json_decode($rs['goodsinfo'], true);
                
                
                
                $rs = M("goods")->field("id,name,thumb")->find($data[$i]["goodsid"]);
                $data[$i]["name"] = $rs["name"];
                $data[$i]["thumb"] = $rs["thumb"];
                $data[$i]["sid"] = $rs["id"];
                
                $rs = M("user")->find($data[$i]["uid"]);
                $data[$i]["nickname"] = $rs["nickname"];
                // $data[$i]["mobile"]=$rs["mobile"];
                $data[$i]["head"] = $rs["head"];
            }
        }
        
        // echo "<pre>";print_r($data);echo "</pre>";
        
        $this->page = $page->show();
        
        $this->data = $data;
        
        $this->display();
    }

    /**
     * @批量同意退款
     */
    public function is_AgreeRefund()
    {
        if (IS_POST) {
            
            $param = I('id');
            
            if (! $param) {
                ajax_return(0, '非法操作');
            }
            
            if (is_array($param)) {
                
                $map['id'] = array(
                    'in',
                    $param
                );
            } else {
                
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            $rs = M("refund")->where($map)->select();
            
            if (! $rs) {
                ajax_return(1, '请选择退款记录');
            }
            
            Vendor('campanyPay.refund');
            
            $pay = new \Refund();
            
            for ($i = 0; $i < count($rs); $i ++) {
                
                $order = M("order")->field("id,pay_jiner,out_trade_no,transaction_id")
                    ->where("out_trade_no='" . $rs[$i]["out_trade_no"] . "'")
                    ->find();
                
                if (! $order) {
                    ajax_return(1, '原始订单记录不存在:' . $rs[$i]["out_trade_no"]);
                }
                
                $pay_jiner = $order["pay_jiner"]; // 订单实际支付的总金额
                
                $no = 'TK' . $order['out_trade_no'] . mt_rand(1, 999); // 生成的退款订单号
                
                $total_fee = $pay_jiner; // 订单实际支付的总金额
                
                $refund_fee = $rs[$i]['jiner']; // 此次订单退款金额
                
                $transaction_id = $order['transaction_id'];
                
                // $res = $pay->pay($transaction_id,'',$no,$total_fee,$refund_fee);
                
                $res = $pay->pay($transaction_id, '', $no, $total_fee, $refund_fee);
                if ($res['status']) {
                    
                    ajax_return(1, '退款失败:' . $res['msg']);
                    exit();
                } else {
                    
                    $arr = array(
                        "status" => 2,
                        "updatetime" => time(),
                        "remark" => "退款成功"
                    );
                    
                    M("refund")->where("id=" . $rs[$i]["id"])->setField($arr);
                    
                    // $sumcount=M("refund")->where("out_trade_no='".$order['out_trade_no']."' and status=2")->sum("jiner");
                    
//                     $arr=array(
//                         "tui_status"=>2,
//                         //"is_tui"=>
//                     );
                    
                    M("order")->where("out_trade_no='".$order["out_trade_no"]."'")->setField("tui_status",2);
                    
//                     $rx = M("refund")->where("out_trade_no='" . $order['out_trade_no'] . "' and status=1")->select();
                    
//                     if (! $rx) {
//                         M("order")->where("out_trade_no='" . $order['out_trade_no'] . "'")->setField("is_tui", 1);
//                     }
                    
                    // if($order["pay_jiner"]>$sumcount){
                    // M("order")->where("out_trade_no='".$order['out_trade_no']."'")->setField("is_tui",3);
                    // }else{
                    // M("order")->where("out_trade_no='".$order['out_trade_no']."'")->setField("is_tui",2);
                    // }
                    
                    M("order")->where("out_trade_no='" . $order['out_trade_no'] . "'")->setInc("refund_jiner", $rs[$i]["jiner"]);
                }
            }
            
            ajax_return(0, "批量退款成功");
        }
    }

    /**
     * @批量不同意退款
     */
    public function not_AgreeRefund()
    {
        // 不同意退款
        $param = I('id');
        
        if (! $param) {
            ajax_return(0, '非法操作');
        }
        
        if (is_array($param)) {
            
            $map['id'] = array(
                'in',
                $param
            );
        } else {
            
            $map['id'] = $param;
        }
        
        $map["uniacid"]=$this->uniacid;
        
        $arr = array(
            "status" => 3,
            "updatetime" => time(),
            "remark" => "不同意退款，有疑问请联系客服"
        );
        
        M("refund")->where($map)->setField($arr);
        
        $order = M("refund")->where($map)->select();
        for ($i = 0; $i < count($order); $i ++) {
            $arr=array(
                "tui_status"=>3,
                "is_tui"=>1
            );
            M("order")->where("out_trade_no='" . $order[$i]['out_trade_no'] . "'")->setField($arr);
        }
        
        ajax_return(0, '拒绝退款操作成功');
    }

    /**
     * @批量删除退款订单操作
     */
    public function refunddel()
    {
        if (IS_POST) {
            
            $param = I('id');
            
            if (is_array($param)) {
                
                $map['id'] = array(
                    'in',
                    $param
                );
            } else {
                
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            M('order')->where($map)->delete();
            
            ajax_return(0, '删除成功');
        }
    }

    /**
     *@服务器端订单核销 
     */
    public function serverHexiao(){
        
        $param=I('id');
        $where="uniacid=".$this->uniacid;
        if(is_array($param)){
            $where.=" and id in (".implode(',',$param).")";
        }else{
            $where.=" and id=".$param;
        }
        $where.=" and is_pay=2";
        $where.=" and is_send=2";
        $where.=" and is_del=1";
        $where.=" and is_complete=1";
        $where.=" and is_tui!=2";
        $where.=" and tui_status!=2";
        
        $orderobj = M('order');
        $hexiao_record = M('hexiao_record');
        
        $data = M('order')->where($where)
                ->field('id,goodsinfo,pay_jiner,out_trade_no,activity_id,xiaoqu_id,total_jiner,peisong_fee,uid,tuan_uid,fee,sendtime,refund_jiner')
                ->select();
        if($data){
            foreach ($data as $k => $v)
            {
        
                    $orderobj->where(['id'=>$v['id']])->save(['is_complete'=>2]);
                    $add['out_trade_no'] = $v['out_trade_no'];
                    $add['uid'] = $v['tuan_uid'];
                    $add['uniacid'] = $this->uniacid;
                    $add['addtime'] = time();
                    $add['type'] = 2;
                    $hexiao_record->add($add);
        
        
                    $money = $v['fee']*($v['total_jiner']-$v['refund_jiner'])/100;
                    
                    $gg = json_decode($v['goodsinfo'],true);
                    $money = 0;
                    if($gg){
                        foreach ($gg as $kk => $vv)
                        {
                            $money+=$vv['tuan_price']*$vv['num']*$vv['fee']/100;
                        }
                    }
                    
        
                    if($money){
                        $add1['uid'] =$v['tuan_uid'];
                        $add1['activity_id'] =$v['activity_id'];
                        $add1['xiaoqu_id'] =$v['xiaoqu_id'];
                        $add1['out_trade_no'] = $v['out_trade_no'];
                        $add1['money'] = $money;
                        $add1['addtime'] = time();
                        $add1['year'] = date('Y');
                        $add1['month'] = date('Ym');
                        $add1['day'] = date('Ymd');
                        $add1['uniacid'] = $this->uniacid;
        
                        $id = M('rewardlog')->add($add1);
        
                        $this->add_min_u($v['tuan_uid'],$money,'brokerage','佣金发放,ID:'.$id);
        
                        $xiaoquids = M('zongtuanzhang')->where(['status'=>1,'uniacid'=>$this->uniacid])->field('uid,uuid')->select();
                        if($xiaoquids){
                            foreach ($xiaoquids as $kk => $vv)
                            {
                                $g= explode(',',$vv['uuid']);
                                if(in_array($v['xiaoqu_id'],$g)){
                                    $money = $money*C('zongtuanzhang_fee')/100;
                                    if($money){
                                        $add2['uid'] =$vv['uid'];
                                        $add2['tuan_uid'] =$v['tuan_uid'];
                                        $add2['activity_id'] =$v['activity_id'];
                                        $add2['xiaoqu_id'] =$v['xiaoqu_id'];
                                        $add2['out_trade_no'] = $add1['out_trade_no'];
                                        $add2['money'] = $money;
                                        $add2['addtime'] = time();
                                        $add2['type'] = 2;
                                        $add2['year'] = date('Y');
                                        $add2['month'] = date('Ym');
                                        $add2['day'] = date('Ymd');
                                        $add2['uniacid'] = $this->uniacid;
                                        $id2 = M('rewardlog')->add($add2);
        
                                        $this->add_min_u($vv['uid'],$money,'brokerage','总团长佣金发放,ID:'.$id2);
        
                                    }
        
                                    break;
                                }
        
                            }
        
                        }
                    }
        
                    if($v['peisong_fee']){
                        
                        $this->add_min_u($v['tuan_uid'],$v['peisong_fee'],'brokerage','配送费,ID:'.$id);
        
                    }
        
        
            }
        }        
        
        ajax_return(0, "核销成功");
        
    }
    
    /*添加佣金明细和添加财务明细*/
    public function add_min_u($uid,$amount,$type,$info,$paytype=1)
    {
    
    
        if($amount>0){
    
            if($paytype == 1){//增加
              //  echo $amount;die;
                M('user')->where(['id'=>$uid])->setInc($type,$amount);
    
            }else{//减少
                M('user')->where(['id'=>$uid])->setDec($type,$amount);
    
            }
    
            $u =M("user")->find($uid);
    
    
    
            $add['uid'] = $uid;
            $add['money'] = $amount;
            $add['balance'] = $u[$type];
            $add['type'] = $paytype;
            $add['addtime'] = time();
            $add['info'] = $info;
            $add['year'] = date('Y');
            $add['month'] = date('Ym');
            $add['day'] = date('Ymd');
            $add['uniacid'] = $this->uniacid;
            return M('caiwu')->add($add);
        }
    }
    

    /* 显示要修改的订单归属小区页面 */
    public function showorderxiaoqu(){
    
        $id=isset($_GET["id"])?intval($_GET['id']) :0;
        if($id<=0){
            ajax_return(1, "非法操作");
        }
        $order=M("order")->find($id);
    
        $this->quyu=M("quyu")->order("id desc")->select();
        $userinfo=M("user")->find($order["uid"]);
        $this->userinfo=$userinfo;
        $tuaninfo=M("xiaoqu")->where("uid=".$order["tuan_uid"])->find();
        $this->tuaninfo=$tuaninfo;
        $quyu=M("quyu")->find($order["quyu_id"]);
        $order["quyu_name"]=$quyu["name"];
        $xiaoqu=M("xiaoqu")->find($order["xiaoqu_id"]);
        $order["xiaoqu_name"]=$xiaoqu["name"];
        $activity=M("activity")->find($order["activity_id"]);
        $order["activity_id"]=$activity["name"];
        $order["goodsinfo"]=json_decode($order["goodsinfo"],true);
        $this->order=$order;
        $this->display();
    }
    
    /*修改订单归属小区提交操作*/
    public function showorderxiaoqu_addhandle(){
    
        $param=I('get.');
        if(!$param){
            ajax_return(1, "非法操作");
        }
        if($param["quyu_id"]==0){
            ajax_return(1, "线路必须要选择");
        }
        if($param["xiaoqu_id"]==0){
            ajax_return(1, "小区必须要选择");
        }
        if($param["id"]<=0){
            ajax_return(1, "非法操作");
        }
        $xiaoqu=M("xiaoqu")->where("quyu_id=".$param["quyu_id"]." and id=".$param["xiaoqu_id"])->find();
        $param["tuan_uid"]=$xiaoqu["uid"];
    
        $rs=M("order")->save($param);
        if($rs){
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }
    }
}